#include "test/globaldef.h"
#include "test/hz512.h"
#include "test/m_penuLoad.h"
pQUEUE_ENTRY m_tranBuffer = NULL;
pQUEUE_ENTRY m_recvBuffer = NULL;
#define SPI_NORMAL      0
#define SPI_SPEC_SEND   1
#define SPI_SPEC_RECV   2
extern BIT8 Valve_On_Off_Mask;
static int fd_hz512;
static int spi_State = SPI_NORMAL;
static char* spi_buffer = NULL;
static char test_buffer[32] = {0};
static pthread_mutex_t hz512_mut     = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t hz512_condmut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  hz512_cond    = PTHREAD_COND_INITIALIZER;
static char isLeveldown = 0;
static void pabort(const char *s)
{
  perror(s);
  abort();
}

void SendBuffer( U_INT16 *buffer )
{
  char temp_buffer[32] = {0};
  InitializeOnchipQSM();
  /* wiggle sclk 8 times to make the dsp serial config take hold  */
  my_SPI_write(temp_buffer, 1);

  my_SPI_setWR(1);//only write
  my_SPI_write(buffer, 32);

  spi_State = SPI_NORMAL;

}
void ReadBuffer( U_INT16 *buffer )
{
  char temp_buffer[32] = {0};

  InitializeOnchipQSM();
  /* wiggle sclk 8 times to make the dsp serial config take hold  */
  my_SPI_write(temp_buffer, 1);

  my_SPI_setWR(2);//only read
  my_SPI_write(temp_buffer, 32);
  my_SPI_read(buffer, 32);
  spi_State = SPI_NORMAL;
}
void SendReadBuffer( U_INT16 *sendbuffer,U_INT16 *readbuffer)
{
  char temp_buffer[32] = {0};  
  InitializeOnchipQSM();
  /* wiggle sclk 8 times to make the dsp serial config take hold  */
  my_SPI_write(temp_buffer, 1);

  my_SPI_setWR(3);// write & read
  my_SPI_write(sendbuffer, 32);
  my_SPI_read(readbuffer, 32);
  spi_State = SPI_NORMAL;
}

void hz512_handler(int num)
{
  int i = 0;
  char mbit = 0;
  pthread_mutex_lock(&hz512_mut); //in case the handler last more than 2ms
  read(fd_hz512,&mbit,1);
  if(mbit == 0)
  {
    ioctl(fd_hz512, 0, 0); //start the next interrupt;
    pthread_mutex_unlock(&hz512_mut);
    return;
  }



//  pthread_mutex_lock(&hz512_mut);  //in case the handler last more than 2ms

  if(spi_State == SPI_SPEC_SEND)  //this is different from old code
  {
    SendBuffer(spi_buffer);
  }
  else if(spi_State == SPI_SPEC_RECV)
  {
    ReadBuffer(spi_buffer);
  }
else if(pthread_mutex_trylock(&queue_mut) != 0)
{
//   printf("unsuccess lock queue_mut!!!!!\n");
}
else if(pthread_mutex_trylock(&recvqueue_mut) != 0)
{
//   printf("unsuccess lock recvqueue_mut!!!!!\n");
   pthread_mutex_unlock(&queue_mut); 
}
else
{
  if(Pneu_Cmd_Queue.Num_Entries !=0)
  {
    if(is_my_SPI_open())
    {

      GetBuffer(m_tranBuffer, &Pneu_Cmd_Queue);

//      mLog("\n");
//      mLog("sending the data\n");

      if(m_tranBuffer != NULL && m_recvBuffer != NULL)
      {
        SendReadBuffer(m_tranBuffer->PCB_Command,m_recvBuffer->PCB_Response);
        LinkBufferToTail(m_recvBuffer, &Pneu_Resp_Queue);
        m_recvBuffer = NULL;
      }
      else if(m_tranBuffer != NULL && m_recvBuffer == NULL)
      {
        SendBuffer(m_tranBuffer->PCB_Command);
      }
//      mLog("finish the sending\n");
    }
    if(m_tranBuffer != NULL)
    {
      m_recvBuffer = m_tranBuffer;
      m_tranBuffer = NULL;
    }
  }
  else if(m_recvBuffer != NULL)
  {
    ReadBuffer(m_recvBuffer->PCB_Response);
    LinkBufferToTail(m_recvBuffer, &Pneu_Resp_Queue);
    m_recvBuffer = NULL;
  }

  pthread_mutex_unlock(&queue_mut); 
  pthread_mutex_unlock(&recvqueue_mut); 
}
  my_SPI_setWR(4);// sol valve signal
  my_SPI_write(&Valve_On_Off_Mask, 1);


  //ioctl(fd_hz512, 0, 0); //continue the interrupt
  pthread_mutex_unlock(&hz512_mut);
  pthread_cond_signal(&hz512_cond);   //tell the waiting task that hz512 has finished
}
int hz512_init()
{
  int oflags;
  fd_hz512 = open("/dev/irq_hz512", O_RDWR);
  if(fd_hz512 < 0)
  {
    mLog("hz512 interrput init fail!!\n");
    return fd_hz512;
  }
  signal(SIGIO, hz512_handler);
  fcntl(fd_hz512, F_SETOWN, getpid());
  oflags = fcntl(fd_hz512, F_GETFL);
  fcntl(fd_hz512, F_SETFL, oflags | FASYNC);
  ioctl(fd_hz512, 0, 0); //start the interrupt
  mLog("hz512 interrupt init successfully\n");
  return 0;
}
void hz512_close()
{
  close(fd_hz512);
  mLog("hz512 interrupt close\n");
}

void SendBufferToPCB( U_INT16 *buffer )
{
  pthread_mutex_lock(&hz512_condmut);
  spi_buffer = buffer;
  spi_State = SPI_SPEC_SEND;
  pthread_cond_wait(&hz512_cond, &hz512_condmut);  //wait 512Hz handle the send
  pthread_mutex_unlock(&hz512_condmut);
}
void ReadBufferFromPCB( U_INT16 *buffer )
{
  pthread_mutex_lock(&hz512_condmut);
  spi_buffer = buffer;
  spi_State = SPI_SPEC_RECV;
  pthread_cond_wait(&hz512_cond, &hz512_condmut);  //wait 512Hz handle the recv
  pthread_mutex_unlock(&hz512_condmut);
}


